package cn.mw.cmdb.util;

import cn.mw.cmdb.entity.DataAuth;
import cn.mw.cmdb.entity.DataAuthType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.microMonitorCommon.entity.common.FieldConditionDto;
import cn.mw.microMonitorCommon.enums.QueryConditionEnum;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.google.common.base.Strings;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.mw.cmdb.entity.PropertyTypeTime.EndTimeKey;
import static cn.mw.cmdb.entity.PropertyTypeTime.StartTimeKey;
import static cn.mw.microMonitorCommon.util.MysqlConditionAssemblerUtil.getDateStartAndEnd;

public class MwCMDBMongoDBApi {

    public static SearchParam getSearchParamList(List<FieldConditionDto> conditionDtos) {
        SearchParam searchParam = new SearchParam();
        if (CollectionUtils.isNotEmpty(conditionDtos)) {
            for (FieldConditionDto conditionDto : conditionDtos) {
                if(conditionDto!=null){
                    String filterRule = conditionDto.getFilterRule();
                    QueryConditionEnum conditionEnum = null;
                    if(!Strings.isNullOrEmpty(filterRule)){
                        conditionEnum = QueryConditionEnum.getConditionEnum(filterRule);
                    }
                    String filterCondition = conditionDto.getFilterCondition();
                    QueryConditionEnum andOrEnum = null;
                    if(!Strings.isNullOrEmpty(filterCondition)){
                        andOrEnum = QueryConditionEnum.getConditionEnum(filterCondition);
                    }
                    if (conditionEnum != null && andOrEnum != null) {
                        switch (andOrEnum) {
                            case OR:
                                searchParam.or();
                                break;
                            default:
                                searchParam.and();
                        }
                        switch (conditionEnum) {
                            case LIKE:
                                searchParam.like(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case NOT_LIKE:
                                searchParam.notLike(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case EQUALS:
                                searchParam.is(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case NOT_EQUALS:
                                searchParam.ne(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case GREATER_THAN:
                                searchParam.gt(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case LESS_THAN:
                                searchParam.lt(conditionDto.getFieldName(), conditionDto.getFilterValue());
                                break;
                            case BETWEEN:
                                searchParam.between(conditionDto.getFieldName(), getBetweenSearch(conditionDto.getFilterValue()));
                                break;
                            default:
                                searchParam.is(CMDBTool.getFieldName(DataAuth::getDataAuthType), DataAuthType.internal.name());
                        }
                    }
                }
            }
        } else {
            //没有查询条件，默认查询全部数据
            searchParam.and().is(CMDBTool.getFieldName(DataAuth::getDataAuthType), DataAuthType.internal.name());
        }
        return searchParam;
    }

    public static Map getBetweenSearch(Object value) {
        List<String> dateStartAndEnd = getDateStartAndEnd(value);
        Map map = new HashMap();
        if (CollectionUtils.isNotEmpty(dateStartAndEnd)) {
            map.put(StartTimeKey, dateStartAndEnd.get(0));
            map.put(EndTimeKey, dateStartAndEnd.get(1));
        }
        return map;
    }
}